<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="6" :xs="24">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>学生信息</span>
          </div>
          <div>
<!--            <div class="text-center">-->
<!--              <userAvatar />-->
<!--            </div>-->
            <ul class="list-group list-group-striped">
              <li class="list-group-item">
                <svg-icon icon-class="user" />学生名称
                <div class="pull-right">{{ schedule.student }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="user" />联系人
                <div class="pull-right">{{ schedule.guardian }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="phone" />联系电话
                <div class="pull-right">{{ schedule.guardianPhone }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="email" />课程
                <div class="pull-right">{{ schedule.subject }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="date" />建档日期
                <div class="pull-right">{{ schedule.createTime }}</div>
              </li>
            </ul>
          </div>
        </el-card>
      </el-col>
      <el-col :span="18" :xs="24">
        <el-card>
          <div slot="header" class="clearfix">
            <span>基本资料</span>
          </div>
          <el-table v-loading="loading" :data="recordList">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="学习时间" align="center" prop="createTime" >
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.createTime) }}</span>
              </template>
            </el-table-column>
            <el-table-column label="学习记录" align="center" prop="studyRecord" />
            <el-table-column label="所属机构名称" align="center" prop="tenantName" />
            <el-table-column label="附件" align="center" prop="attachment" >
              <template slot-scope="scope">
                <el-image  :src="getFullUrl(scope.row.attachment)"   :preview-src-list="getFullUrlList(scope.row.attachment)"  />
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-card>


      </el-col>
    </el-row>
  </div>
</template>

<script>
import { listRecord, getRecord, delRecord, addRecord, updateRecord } from "@/api/edu/record";
import { getSchedule } from "@/api/edu/studentSchedule";

export default {
  name: "Record",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学习记录表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentId: null,
        student: null,
        subjectId: null,
        subject: null,
        studyRecord: null,
        tenantId: null,
        tenantName: null,
        attachment: null
      },
      // 表单参数
      form: {},
      schedule: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    const scheduleId = this.$route.params && this.$route.params.scheduleId;
    this.queryParams.scheduleId = scheduleId;
    this.getList();
    this.getScheduleInfo(scheduleId);
  },
  methods: {
    // 获取完整 URL
    getFullUrlList(path) {
      const url = process.env.VUE_APP_BASE_API + path;
      return [url];
    },
    getFullUrl(path) {
      return process.env.VUE_APP_BASE_API + path;
    },
    getScheduleInfo(scheduleId) {
      getSchedule(scheduleId).then(response => {
        this.schedule = response.data
      })
    },
    /** 查询学习记录列表 */
    getList() {
      this.loading = true;
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        studentId: null,
        student: null,
        subjectId: null,
        subject: null,
        studyRecord: null,
        tenantId: null,
        tenantName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        attachment: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学习记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRecord(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学习记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除学习记录编号为"' + ids + '"的数据项？').then(function() {
        return delRecord(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('edu/record/export', {
        ...this.queryParams
      }, `record_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
